import { asyncRoutes, constantRoutes } from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes

      switch (roles) {
        case 1:
          accessedRoutes = asyncRoutes || []
          break
        case 3:
          accessedRoutes = asyncRoutes.filter(item => {
            if (item.path == '/information' || item.path == '/hqinformation') {
              return true
            }
          })
          break
        case 5:
          console.log(asyncRoutes)
          accessedRoutes = asyncRoutes.filter(item => {
            if (item.path == '/video' || item.path == '/hqinformation' || item.path == '/news' || item.path == '/service' || item.path == '/hqdirectSeeding') {
              return true
            }
          })
          break
        case 6:

          accessedRoutes = asyncRoutes.filter(item => {
            if (item.path == '/order') {
              const children = item.children.filter(son => (son.path == 'consumeOrder'))

              item.redirect = '/order/consumeOrder'
              item.children = children
            }

            //  item.children = children;

            if (item.path == '/hqinformation' || item.path == '/fourteen' || item.path == '/order') {
              return true
            }
          })

          // accessedRoutes = accessedRoutes.map(item => {

          //   // item.children = item.children.filter(item => {
          //   //   if (item.path != 'consumeOrder') {
          //   //     return true
          //   //   }
          //   // })
          //   return {
          //     ...item
          //   }
          // })

          console.log(accessedRoutes)

          break

        case 7:

          accessedRoutes = asyncRoutes.filter(item => {
            if (item.path == '/news' || item.path == '/video' || item.path == '/userManagement') {
              return true
            }

            //  item.children = children;
          })

          // accessedRoutes = accessedRoutes.map(item => {

          //   // item.children = item.children.filter(item => {
          //   //   if (item.path != 'consumeOrder') {
          //   //     return true
          //   //   }
          //   // })
          //   return {
          //     ...item
          //   }
          // })

          console.log(accessedRoutes)

          break
      }

      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
